package c.e.b.h.e.k;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.hardware.SensorManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Debug;
import android.os.StatFs;
import android.provider.Settings.Secure;
import android.text.TextUtils;
import android.util.Log;
import c.e.b.h.e.p.c;
import com.pgl.sys.ces.out.ISdkLite;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;

/* compiled from: CommonUtils */
public class g {
    public static final char[] a = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    public static long b = -1;
    public static final Comparator<File> c = new a();

    /* compiled from: CommonUtils */
    public class a implements Comparator<File> {
        public int compare(Object obj, Object obj2) {
            return (int) (((File) obj).lastModified() - ((File) obj2).lastModified());
        }
    }

    /* compiled from: CommonUtils */
    public enum b {
        X86_32,
        X86_64,
        ARM_UNKNOWN,
        PPC,
        PPC64,
        ARMV6,
        ARMV7,
        UNKNOWN,
        ARMV7S,
        ARM64;
        
        public static final Map<String, b> k = null;

        static {
            HashMap hashMap = new HashMap(4);
            k = hashMap;
            hashMap.put("armeabi-v7a", r11);
            hashMap.put("armeabi", r9);
            hashMap.put("arm64-v8a", r14);
            hashMap.put("x86", r0);
        }
    }

    public static long a(String str) {
        StatFs statFs = new StatFs(str);
        long blockSize = (long) statFs.getBlockSize();
        return (((long) statFs.getBlockCount()) * blockSize) - (blockSize * ((long) statFs.getAvailableBlocks()));
    }

    @SuppressLint({"MissingPermission"})
    public static boolean b(Context context) {
        boolean z = false;
        if ((context.checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STATE") == 0 ? 1 : null) == null) {
            return true;
        }
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting()) {
            z = true;
        }
        return z;
    }

    public static void c(Closeable closeable, String str) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                if (c.e.b.h.e.b.a.a(6)) {
                    Log.e("FirebaseCrashlytics", str, e);
                }
            }
        }
    }

    public static long d(String str, String str2, int i) {
        return Long.parseLong(str.split(str2)[0].trim()) * ((long) i);
    }

    public static String e(String... strArr) {
        if (strArr.length != 0) {
            ArrayList arrayList = new ArrayList();
            for (String str : strArr) {
                if (str != null) {
                    arrayList.add(str.replace("-", "").toLowerCase(Locale.US));
                }
            }
            Collections.sort(arrayList);
            StringBuilder stringBuilder = new StringBuilder();
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                stringBuilder.append((String) it.next());
            }
            String stringBuilder2 = stringBuilder.toString();
            if (stringBuilder2.length() > 0) {
                return t(stringBuilder2);
            }
        }
        return null;
    }

    public static String f(File file, String str) {
        Throwable e;
        c.e.b.h.e.b bVar;
        StringBuilder stringBuilder;
        Throwable th;
        Closeable closeable;
        String str2 = "Failed to close system file reader.";
        String str3 = null;
        if (file.exists()) {
            Closeable bufferedReader;
            try {
                bufferedReader = new BufferedReader(new FileReader(file), 1024);
                while (true) {
                    try {
                        String readLine = bufferedReader.readLine();
                        if (readLine == null) {
                            break;
                        }
                        String[] split = Pattern.compile("\\s*:\\s*").split(readLine, 2);
                        if (split.length > 1 && split[0].equals(str)) {
                            str3 = split[1];
                            break;
                        }
                    } catch (Exception e2) {
                        e = e2;
                        try {
                            bVar = c.e.b.h.e.b.a;
                            stringBuilder = new StringBuilder();
                            stringBuilder.append("Error parsing ");
                            stringBuilder.append(file);
                            bVar.e(stringBuilder.toString(), e);
                            c(bufferedReader, str2);
                            return str3;
                        } catch (Throwable th2) {
                            th = th2;
                            closeable = bufferedReader;
                            c(closeable, str2);
                            throw th;
                        }
                    }
                }
            } catch (Exception e3) {
                e = e3;
                bufferedReader = null;
                bVar = c.e.b.h.e.b.a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("Error parsing ");
                stringBuilder.append(file);
                bVar.e(stringBuilder.toString(), e);
                c(bufferedReader, str2);
                return str3;
            } catch (Throwable th3) {
                th = th3;
                c(closeable, str2);
                throw th;
            }
            c(bufferedReader, str2);
        }
        return str3;
    }

    public static void g(Flushable flushable, String str) {
        if (flushable != null) {
            try {
                ((c) flushable).flush();
            } catch (IOException e) {
                if (c.e.b.h.e.b.a.a(6)) {
                    Log.e("FirebaseCrashlytics", str, e);
                }
            }
        }
    }

    public static RunningAppProcessInfo h(String str, Context context) {
        List<RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses();
        if (runningAppProcesses != null) {
            for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
                if (runningAppProcessInfo.processName.equals(str)) {
                    return runningAppProcessInfo;
                }
            }
        }
        return null;
    }

    public static boolean i(Context context, String str, boolean z) {
        if (context != null) {
            Resources resources = context.getResources();
            if (resources != null) {
                int m = m(context, str, "bool");
                if (m > 0) {
                    return resources.getBoolean(m);
                }
                int m2 = m(context, str, "string");
                if (m2 > 0) {
                    return Boolean.parseBoolean(context.getString(m2));
                }
            }
        }
        return z;
    }

    public static int j(Context context) {
        Object obj = 1;
        int i = q(context) ? 1 : 0;
        if (s(context)) {
            i |= 2;
        }
        if (!(Debug.isDebuggerConnected() || Debug.waitingForDebugger())) {
            obj = null;
        }
        return obj != null ? i | 4 : i;
    }

    public static String k(Context context) {
        String str = "string";
        int m = m(context, "com.google.firebase.crashlytics.mapping_file_id", str);
        if (m == 0) {
            m = m(context, "com.crashlytics.android.build_id", str);
        }
        return m != 0 ? context.getResources().getString(m) : null;
    }

    public static boolean l(Context context) {
        boolean z = false;
        if (q(context)) {
            return false;
        }
        if (((SensorManager) context.getSystemService("sensor")).getDefaultSensor(8) != null) {
            z = true;
        }
        return z;
    }

    public static int m(Context context, String str, String str2) {
        String resourcePackageName;
        Resources resources = context.getResources();
        int i = context.getApplicationContext().getApplicationInfo().icon;
        if (i > 0) {
            try {
                resourcePackageName = context.getResources().getResourcePackageName(i);
                if ("android".equals(resourcePackageName)) {
                    resourcePackageName = context.getPackageName();
                }
            } catch (NotFoundException unused) {
                resourcePackageName = context.getPackageName();
            }
        } else {
            resourcePackageName = context.getPackageName();
        }
        return resources.getIdentifier(str, str2, resourcePackageName);
    }

    public static SharedPreferences n(Context context) {
        return context.getSharedPreferences("com.google.firebase.crashlytics", 0);
    }

    public static synchronized long o() {
        long j;
        c.e.b.h.e.b bVar = c.e.b.h.e.b.a;
        synchronized (g.class) {
            if (b == -1) {
                j = 0;
                String f = f(new File("/proc/meminfo"), "MemTotal");
                if (!TextUtils.isEmpty(f)) {
                    f = f.toUpperCase(Locale.US);
                    try {
                        if (f.endsWith("KB")) {
                            j = d(f, "KB", 1024);
                        } else if (f.endsWith("MB")) {
                            j = d(f, "MB", 1048576);
                        } else if (f.endsWith("GB")) {
                            j = d(f, "GB", 1073741824);
                        } else {
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append("Unexpected meminfo format while computing RAM: ");
                            stringBuilder.append(f);
                            bVar.b(stringBuilder.toString());
                        }
                    } catch (NumberFormatException e) {
                        StringBuilder stringBuilder2 = new StringBuilder();
                        stringBuilder2.append("Unexpected meminfo format while computing RAM: ");
                        stringBuilder2.append(f);
                        bVar.e(stringBuilder2.toString(), e);
                    }
                }
                b = j;
            }
            j = b;
        }
        return j;
    }

    public static String p(byte[] bArr) {
        char[] cArr = new char[(bArr.length * 2)];
        for (int i = 0; i < bArr.length; i++) {
            int i2 = bArr[i] & ISdkLite.REGION_UNSET;
            int i3 = i * 2;
            char[] cArr2 = a;
            cArr[i3] = cArr2[i2 >>> 4];
            cArr[i3 + 1] = cArr2[i2 & 15];
        }
        return new String(cArr);
    }

    public static boolean q(Context context) {
        String string = Secure.getString(context.getContentResolver(), "android_id");
        String str = Build.PRODUCT;
        return "sdk".equals(str) || "google_sdk".equals(str) || string == null;
    }

    public static boolean r(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean s(Context context) {
        boolean q = q(context);
        String str = Build.TAGS;
        if ((!q && str != null && str.contains("test-keys")) || new File("/system/app/Superuser.apk").exists()) {
            return true;
        }
        File file = new File("/system/xbin/su");
        if (q || !file.exists()) {
            return false;
        }
        return true;
    }

    public static String t(String str) {
        String str2 = "SHA-1";
        byte[] bytes = str.getBytes();
        try {
            str2 = MessageDigest.getInstance(str2);
            str2.update(bytes);
            return p(str2.digest());
        } catch (NoSuchAlgorithmException e) {
            c.e.b.h.e.b bVar = c.e.b.h.e.b.a;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Could not create hashing algorithm: ");
            stringBuilder.append(str2);
            stringBuilder.append(", returning empty string.");
            bVar.e(stringBuilder.toString(), e);
            return "";
        }
    }

    public static String u(InputStream inputStream) throws IOException {
        Scanner useDelimiter = new Scanner(inputStream).useDelimiter("\\A");
        return useDelimiter.hasNext() ? useDelimiter.next() : "";
    }
}
